package com.test_randomjokes.slice;

import com.test_randomjokes.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class MainAbilitySlice extends AbilitySlice {
    Text text;
    Button button;
    BufferedReader bs;
    StringBuffer stringBuffer;
    String[] split;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
       text = (Text) this.findComponentById(ResourceTable.Id_jockcontent);
      button = (Button) this.findComponentById(ResourceTable.Id_change_button);
      stringBuffer = new StringBuffer();
        try {
            Resource jock = this.getResourceManager().getResource(ResourceTable.Profile_joke);
            bs = new BufferedReader(new InputStreamReader(jock));
           String line =null;
            while ((line=bs.readLine())!=null){
                stringBuffer.append(line);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        }
      split = stringBuffer.toString().split("---"); stringBuffer.toString().split("---");

        button.setClickedListener(new Component.ClickedListener() {
          @Override
          public void onClick(Component component) {
              Random random = new Random();
              int i = random.nextInt(split.length);
              text.setText(split[i]);
          }
      });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
